import 'package:flutter/material.dart';

class Product{
  final String title; //商品标题
  final String  description; //商品描述
  Product(this.title, this.description);
}

void main(){
  runApp(MaterialApp(
    title:'导航的数据传递和接收' ,
    home: ProductList(
      products:List.generate(20, (i) => Product('商品 $i','这是一个商品详情，编号为$i'))
    ),
  ));
}
class ProductList extends StatelessWidget {
  final List<Product> products;
  //构造函数
  ProductList({Key key, @required this.products}):super(key:key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title:Text('商品列表')),
      body: ListView.builder(
        itemCount: products.length,
        itemBuilder: (context,index){
          return ListTile(
            title: Text(products[index].title),
            // 点击时候的响应事件
            onTap:(){
              // 导航组件
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context)=>ProductDetail(product:products[index])
                ),
              );
            } ,
          );
        }
      ),
    );
  }
}

class ProductDetail extends StatelessWidget {
  final Product product;
  ProductDetail({Key key, @required this.product}):super(key:key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title:Text('${product.title}')),
      body: Center(
        child: Text("${product.description}"),
      ),
    );
  }
}